package com.example.socialmediaplatform.controller;

import com.example.socialmediaplatform.model.OutputMessage;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class MessageWebSocketController {

    @MessageMapping("/chat.sendMessage")
    @SendTo("/topic/public")
    public OutputMessage sendMessage(OutputMessage message) {
        String time = new SimpleDateFormat("HH:mm").format(new Date());
        return new OutputMessage(
            message.getFromUser() != null ? message.getFromUser() : message.getFrom(),
            message.getContent() != null ? message.getContent() : message.getMessage(),
            time,
            false
        );
    }

    @MessageMapping("/chat.addUser")
    @SendTo("/topic/public")
    public OutputMessage addUser(OutputMessage message) {
        String time = new SimpleDateFormat("HH:mm").format(new Date());
        return new OutputMessage(
            message.getFromUser() != null ? message.getFromUser() : message.getFrom(),
            message.getContent() != null ? message.getContent() : message.getMessage(),
            time,
            false
        );
    }
} 
